﻿using System.IO;
using System.Collections.Generic;

namespace Jinndev {

    public class CacheManager {

        public static string Root { get; private set; }
        public static string CurDir { get; private set; }
        public static bool Initialized { get; private set; }

        private static List<string> dirs = new List<string>();


        public static void Init(string cacheRoot) {
            //string storagePath = StorageUtill.GetStoragePath();
            //Root = Path.Combine(storagePath, "Cache");
            SetRoot(cacheRoot);
            RefreshCurPath();
            Initialized = true;
        }

        public static void SetRoot(string cacheRoot) {
            Root = cacheRoot;
            if(string.IsNullOrEmpty(Root) || !Directory.Exists(Root)) {
                string storagePath = StorageUtill.GetStoragePath();
                Root = Path.Combine(storagePath, "Cache");
            }
        }

        public static void RefreshCurPath() {
            string now = System.DateTime.Now.ToString("yyyy-MM-dd");
            CurDir = Path.Combine(Root, now);
            StorageUtill.PrepairDir(CurDir);

            dirs.Clear();
            dirs.AddRange(Directory.GetDirectories(Root));
            dirs.Sort((a, b) => {
                return b.CompareTo(a);
            });
        }

        public static string GetCurCachePath() {
            return CurDir;
        }


        public static string GetFileName(string id, string ext = null) {
            return ext == null ? id : id + "." + ext;
        }

        // 得到文件路径，不保证存在
        public static string GetFilePath(string id, string ext = null) {
            string cacheName = GetFileName(id, ext);
            return Path.Combine(CurDir, cacheName);
        }

        // 如果存在文件，则得到其路径
        public static bool GetFilePath(out string filePath, string id, string ext = null) {
            if (string.IsNullOrEmpty(id)) {
                filePath = null;
                return false;
            }
            string cacheName = GetFileName(id, ext);

            foreach (string dir in dirs) {
                string path = Path.Combine(dir, cacheName);
                if (StorageUtill.Exists(path)) {
                    filePath = path;
                    return true;
                }
            }
            filePath = null;
            return false;
        }

        public static void Add(string id, byte[] data, string ext = null) {
            if (string.IsNullOrEmpty(id)) {
                return;
            }
            string cacheName = GetFileName(id, ext);
            string path = Path.Combine(CurDir, cacheName);
            StorageUtill.WriteBytes(path, data);
        }

        public static void Delete(string id, string ext = null) {
            if (string.IsNullOrEmpty(id)) {
                return;
            }
            string cacheName = GetFileName(id, ext);

            foreach (string dir in dirs) {
                string path = Path.Combine(dir, cacheName);
                if (StorageUtill.Exists(path)) {
                    StorageUtill.Delete(path);
                }
            }
        }

        public static byte[] Get(string id, string ext = null) {
            string cacheName = GetFileName(id, ext);

            foreach (string dir in dirs) {
                string path = Path.Combine(dir, cacheName);
                if (StorageUtill.Exists(path)) {
                    return StorageUtill.ReadBytes(path);
                }
            }
            return null;
        }

        public static long GetFileLength(string id, string ext = null) {
            string cacheName = GetFileName(id, ext);

            foreach (string dir in dirs) {
                string path = Path.Combine(dir, cacheName);
                if (StorageUtill.Exists(path)) {
                    return StorageUtill.Length(path);
                }
            }
            return 0;
        }

        // 删除n天前的缓存
        public static void DeleteByDay(int days) {
            string date = System.DateTime.Now.AddDays(-days).ToString("yyyy-MM-dd");

            foreach (string dir in dirs) {
                if (Path.GetFileName(dir).CompareTo(date) <= 0) {
                    Directory.Delete(dir, true);
                }
            }
        }

        // 得到n天前的缓存文件夹
        public static List<string> GetDirsByDay(int days) {
            List<string> list = new List<string>();
            string date = System.DateTime.Now.AddDays(-days).ToString("yyyy-MM-dd");

            foreach (string dir in dirs) {
                if (Path.GetFileName(dir).CompareTo(date) <= 0) {
                    list.Add(dir);
                }
            }
            return list;
        }

        public static bool Exists(string id, string ext = null) {
            string cacheName = GetFileName(id, ext);

            foreach (string dir in dirs) {
                string path = Path.Combine(dir, cacheName);
                if (StorageUtill.Exists(path)) {
                    return true;
                }
            }

            return false;
        }

        public bool Exists(string id, string ext, out string cacheFile) {
            string cacheName = GetFileName(id, ext);

            foreach (string dir in dirs) {
                string path = Path.Combine(dir, cacheName);
                if (StorageUtill.Exists(path)) {
                    cacheFile = path;
                    return true;
                }
            }

            cacheFile = null;
            return false;
        }

        public static int GetFileCount() {
            string path = CurDir;
            if (!Directory.Exists(path)) {
                return 0;
            }
            return Directory.GetFiles(path).Length;
        }


    }

}